#include "srloginpage.h"
#include <QGridLayout>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QRegExp>
#include <QRegExpValidator>
#include <QKeyEvent>
#include <QDebug>

// OE
#include <OEMessageBox>

// private library
#include <auditcore.h>
SRLoginPage::SRLoginPage(QWidget *parent) : QWidget(parent)
{
    setWindowTitle(tr("用户登录"));

    QWidget* wdgt_logo = new QWidget();
    QLabel* lab_logo = new QLabel();
    lab_logo->setAlignment(Qt::AlignCenter);
    lab_logo->setPixmap(QPixmap(":/lab/logo"));
    QLabel* lab_logo_title = new QLabel("复审员登录");
    lab_logo_title->setAlignment(Qt::AlignCenter);
    lab_logo_title->setObjectName(QString("LogoTitle"));

    editUser_ = new QLineEdit();
    QLabel* labUser_ = new QLabel(tr("用户账号"));

    editPswd_ = new QLineEdit();
    QLabel* labPswd_ = new QLabel(tr("登录密码"));
    QLabel* lab_forget = new QLabel(tr("<a href=\"forget\">忘记密码</a>"));

    btnLogin_ = new QPushButton(tr("登录账号"));
    btnRegister_ = new QPushButton(tr("注册账号"));

    QVBoxLayout* layout = new QVBoxLayout();
    QHBoxLayout* layout_user = new QHBoxLayout();
    QHBoxLayout* layout_pswd = new QHBoxLayout();
    QHBoxLayout* layout_btn = new QHBoxLayout();
    QGridLayout* layout_logo = new QGridLayout();



    // 输入限制规则
    QRegExp regQuote("[0-9a-zA-Z]{1,12}");
    QRegExpValidator* quoteValidator = new QRegExpValidator(regQuote, this);


    // 属性设置
    editUser_->setPlaceholderText(tr("请输入账号"));
    editPswd_->setPlaceholderText(tr("请输入密码"));
    editPswd_->setEchoMode(QLineEdit::Password);

    // 设置输入限制
    editUser_->setValidator(quoteValidator);
    editPswd_->setValidator(quoteValidator);

    // 规格
    wdgt_logo->setFixedSize(200,130);
    editUser_->setFixedSize(160,30);
    editPswd_->setFixedSize(160,30);
    btnLogin_->setFixedSize(100,40);
    btnRegister_->setFixedSize(100,40);


    // 布局
    layout_logo->addWidget(lab_logo,0,1,1,1);
    layout_logo->addWidget(lab_logo_title,1,1,1,1);
    wdgt_logo->setLayout(layout_logo);
    layout_user->addWidget(labUser_);
    layout_user->addWidget(editUser_);
    layout_pswd->addWidget(labPswd_);
    layout_pswd->addWidget(editPswd_);
    layout_btn->addWidget(btnLogin_);
    layout_btn->addWidget(btnRegister_);
    layout->addWidget(wdgt_logo,0,Qt::AlignHCenter);
    layout->addLayout(layout_user);
    layout->addLayout(layout_pswd);
    layout->setSpacing(10);
    layout->addWidget(lab_forget, 0, Qt::AlignRight);
    layout->setSpacing(10);
    layout->addLayout(layout_btn);

    QWidget* mainWidget = new QWidget();
    mainWidget->setFixedSize(240,330);
    mainWidget->setLayout(layout);

    // @marker: 因为登录界面比较简单，没有外窗口
    //          所以这边模拟了一个外窗口
    QGridLayout* temp_layout = new QGridLayout();
    temp_layout->addWidget(mainWidget);
    setLayout(temp_layout);


    // 信号链接
    connect(lab_forget, &QLabel::linkActivated,
            this, [=](const QString&){
        emit this->sigForgetPswd();
    });
    connect(btnRegister_, &QPushButton::clicked,
            this, [=]{
        emit this->sigRegister();
    });
    connect(btnLogin_,SIGNAL(clicked()),
            this,SLOT(onLogin()));
}

SRLoginPage::~SRLoginPage()
{
#ifdef QT_DEBUG
    qDebug() << "登录窗口被析构";
#endif
}

void SRLoginPage::onLogin() {
    if (this->editUser_->text().isEmpty()){
        this->editUser_->setFocus();
        OEMessageBox::BriefMessage(this,tr("请填写正确的账号"));
        return;
    }
    else if (this->editPswd_->text().size() < 6) {
        if (this->editPswd_->text().isEmpty())
            this->editPswd_->setFocus();
        OEMessageBox::BriefMessage(this,tr("请填写正确的密码"));
        return;
    }

    emit this->sigLogin(this->editUser_->text(),
                        this->editPswd_->text());

    // 验证登录
    char ch_username[256] = {};
    int ret = apiLoginAccount(this->editUser_->text().toUtf8().data(),
                    this->editPswd_->text().toUtf8().data(),
                              ch_username);

    if (0 != ret) {
        OEMessageBox::BriefMessage(this,tr("账号或密码错误"));
        return;
    }

    emit sigLoginFinshed(this->editUser_->text(),
                         QString::fromUtf8(ch_username));
#ifdef QT_DEBUG
    qDebug() << this->editUser_->text() << this->editPswd_->text();
#endif
}



void SRLoginPage::keyPressEvent(QKeyEvent *event) {
    if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
        onLogin();
        event->accept();
    }
    else if (event->key() == Qt::Key_Escape) {
        QLineEdit* edit = qobject_cast<QLineEdit*>(this->focusWidget());
        if (edit == Q_NULLPTR)
            return;

        edit->clear();
        event->accept();
    }
}
